package org.cqepc.project.reform.controller;

import com.alibaba.fastjson2.util.DateUtils;
import org.cqepc.project.reform.model.entity.UserEntity;
import org.cqepc.project.reform.service.UserService;
import org.cqepc.project.reform.utils.result.Result;
import org.cqepc.project.reform.utils.util.SerialNoGenerateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestParam(value = "username") String userName,
                        @RequestParam(value = "password") String password) {
        UserEntity userEntity = userService.login(userName);

        if (!Objects.isNull(userEntity)) {
            if (Objects.equals(userEntity.getPassword(), password)) {
                return Result.ok(userEntity);
            } else {
                return Result.fail("用户名或密码错误");
            }
        } else {
            return Result.fail("未查询到用户名相关信息");
        }
    }

    public Result logout() {
        return Result.ok("");
    }

    @GetMapping("/get")
    @ResponseBody
    public Result getUser() {
        List<UserEntity> users = userService.getUsers();
        return Result.ok(users);
    }

    @PostMapping("/update")
    @ResponseBody
    public Result setUser(@RequestBody UserEntity user) {
        UserEntity userEntity = userService.updateUser(user);

        return Result.ok(Objects.requireNonNullElse(userEntity, "更新失败"));
    }

    @GetMapping("/delete")
    @ResponseBody
    public Result deleteUser(@RequestParam(value = "userId") String userId) {
        Integer i = userService.deleteUser(userId);
        if (i > 0) {
            return Result.ok("成功");
        }
        return Result.ok("失败");
    }

    @PostMapping("/insert")
    @ResponseBody
    public Result addUser(@RequestBody UserEntity user) {
        // 获取当前ID
        String maxRoundCode = userService.getMaxRoundCode();

        String code = "USER-" + DateUtils.format(new Date(), "yyyyMMdd");
        String newCode = null;
        if (StringUtils.hasLength(maxRoundCode)) {
            // 有长度则不为空
            String getMaxCode = maxRoundCode.substring(13, 15);
            try {
                newCode = code + SerialNoGenerateUtil.getSerialNo(getMaxCode);
            } catch (Exception e) {
                return Result.fail("轮次编号已达上限");
            }
        } else {
            newCode = code + "01";
        }

        user.setUserId(newCode);

        //设置创建和更新时间
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        UserEntity userEntity = userService.addUser(user);

        if (userEntity != null) {
            return Result.ok(userEntity);
        } else {
            return Result.fail("插入用户失败");
        }
    }

}
